/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Engine2D.network;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Garys
 */
public class MulticastListener extends Thread {

    public static class Server {

        private String name;
        private String address;
        private int port;

        public Server(String name, String address, int port) {
            this.name = name;
            this.address = address;
            this.port = port;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getPort() {
            return port;
        }

        public void setPort(int port) {
            this.port = port;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Server other = (Server) obj;
            if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
                return false;
            }
            if ((this.address == null) ? (other.address != null) : !this.address.equals(other.address)) {
                return false;
            }
            if (this.port != other.port) {
                return false;
            }
            return true;
        }

        @Override
        public int hashCode() {
            int hash = 7;
            hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0);
            hash = 59 * hash + (this.address != null ? this.address.hashCode() : 0);
            hash = 59 * hash + this.port;
            return hash;
        }
    }
    private long FIVE_SECONDS = 5000;
    private static boolean listening = true;
    private MulticastSocket socket = null;
    private InetAddress address = InetAddress.getByName("230.0.0.1");
    private ArrayList<Server> servers = new ArrayList<Server>();

    public MulticastListener(int port) throws IOException {
        socket = new MulticastSocket(port);
    }

    public MulticastListener(String address, int port) throws IOException {
        socket = new MulticastSocket(port);
        this.address = InetAddress.getByName(address);
    }

    @Override
    public void run() {
        try {
            socket.joinGroup(address);
            DatagramPacket packet = null;

            // get a few quotes
            while (listening) {


                byte[] buf = new byte[256];
                packet = new DatagramPacket(buf, buf.length);

                socket.receive(packet);

                String received = new String(packet.getData(), 0, packet.getLength());
                addServer(received);
            }

            socket.leaveGroup(address);
            socket.close();
        } catch (IOException ex) {
            Logger.getLogger(MulticastListener.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    

    private void addServer(String received) {

        if (received == null) {
            return;
        }

        String[] recievedString = received.split(",");


        Server recievedServer = new Server(recievedString[0], recievedString[1], Integer.parseInt(recievedString[2]));

        for (Server server : servers) {
            if (server.equals(recievedServer)) {
                return;
            }
        }

        servers.add(recievedServer);
    }

    protected ArrayList<Server> getServers() {
        return servers;
    }
}
